package com.example.controller;

import com.example.service.ProblemService;
import com.example.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description //TODO
 * @Date 2022/9/13 19:52
 * @Author lonng
 **/
@ResponseBody
@Controller
@RequestMapping("problem")
public class ProblemController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProblemService problemService;



    @GetMapping
    public String findAll(){
        if(redisTemplate.opsForValue().size("AllProblems")==0) {
            redisTemplate.opsForValue().set("AllProblems",problemService.findAll() );
            return (String) redisTemplate.opsForValue().get("AllProblems");
        }
        return (String) redisTemplate.opsForValue().get("AllProblems");
    }

    @GetMapping("{tName}")
    public String findAllByTid(@PathVariable("tName")String tName){
        if(redisTemplate.opsForValue().size("ProblemsByTid")==0) {
            redisTemplate.opsForValue().set("ProblemsByTid",problemService.findAllByTid(tName) );
            return (String) redisTemplate.opsForValue().get("ProblemsByTid");
        }
        return (String) redisTemplate.opsForValue().get("ProblemsByTid");
    }

    @GetMapping("difficulty/{difficulty}")
    public String findAllByDifficulty(@PathVariable("difficulty")Integer difficulty){
        if(redisTemplate.opsForValue().size("ProblemsByDifficulty")==0) {
            redisTemplate.opsForValue().set("ProblemsByDifficulty",problemService.findAllDifficulty(difficulty) );
            return (String) redisTemplate.opsForValue().get("ProblemsByDifficulty");
        }
        return (String) redisTemplate.opsForValue().get("ProblemsByDifficulty");
    }

    @GetMapping("titleContains/{title}")
    public String findAllByTitleContains(@PathVariable("title")String title){
//        if(redisTemplate.opsForValue().size("ProblemsByTitleContains")==0) {
//            redisTemplate.opsForValue().set("ProblemsByTitleContains",);
//            return (String) redisTemplate.opsForValue().get("ProblemsByTitleContains");
//        }
        return problemService.findAllByTitleContains(title) ;
    }

}
